﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace PageTest
{
    public class RelayCommand<T>: ICommand
    {
        private Action<T> _execute;

        private Func<object, bool> _canExecute;

        public event EventHandler CanExecuteChanged;

        public RelayCommand(Action<T> execute, Func<object, bool> canExecute)
        {
            _execute = execute;
            _canExecute = canExecute;
        }

        public RelayCommand(Action<T> execute):this(execute,null)
        {
            
        }

        public bool CanExecute(object parameter)
        {
            return (bool)_canExecute?.Invoke(parameter);
        }

        public void Execute(object parameter)
        {
            if (parameter is T)
            {
                _execute.Invoke((T)parameter); 
            }
        }

        //public void Execute(T param)
        //{
        //    _execute.Invoke(param);
        //}
    }
}
